package com.jxbeidou.system.dict.controller;

import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.dict.convert.DictTypeConvert;
import com.jxbeidou.system.dict.entity.DictTypeDO;
import com.jxbeidou.system.dict.service.DictTypeService;
import com.jxbeidou.system.dict.query.DictTypeQuery;
import com.jxbeidou.system.dict.vo.DictTypeVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* 字典类型表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/dict-type")
@AllArgsConstructor
public class DictTypeController {
    @Resource
    private DictTypeService dictTypeService;

    @PostMapping("/page")
    public R<PageResult<DictTypeVO>> page(@RequestBody @Valid DictTypeQuery query){
        PageResult<DictTypeVO> page = dictTypeService.page(query);
        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<DictTypeVO> get(@PathVariable("id") Long id){
        DictTypeDO entity = dictTypeService.getById(id);
        return R.success(DictTypeConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public R<String> save(@RequestBody DictTypeVO vo){
        dictTypeService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    public R<String> update(@RequestBody @Valid DictTypeVO vo){
        dictTypeService.update(vo);
        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        dictTypeService.delete(Arrays.asList(id));
        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        dictTypeService.delete(idList);
        return R.deleteSuccess();
    }
}